package org.flhy.ext.utils;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * <H3></H3>
 * <p></p >
 *
 * @author yiwen zhai
 * @date 2020/9/12
 * @time 1:39 下午
 * @description No Description
 * @since 1.0
 */
@SpringBootConfiguration
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        if (null == ApplicationContextUtil.context) {
            context = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    public static ApplicationContext getContext() {
        return context;
    }

    /**
     * 通过class 获取bean
     *
     * @param clazz {@link Class}
     * @param <T>   T
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过名字获取bean
     *
     * @param name SpringApplication name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
}
